package com.wlb365.financing.modules.esign.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.financing.common.constants.ApiConstants;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.common.exception.AdminException;
import com.wlb365.financing.modules.esign.response.DownFlowDocRes;
import com.wlb365.financing.modules.esign.response.EsignQueryPageRes;
import com.wlb365.financing.modules.esign.response.FlowSignerDto;
import com.wlb365.financing.modules.esign.response.QueryFlowSignersRes;
import com.wlb365.financing.modules.esign.response.QueryPageSignFlowDto;
import com.wlb365.financing.modules.esign.response.QuerySignAntPushInfoDto;
import com.wlb365.financing.modules.esign.response.QuerySignAntPushInfoRes;
import com.wlb365.financing.modules.esign.service.ESignService;
import com.wlb365.financing.modules.esign.vo.EsignQueryVO;
import com.wlb365.financing.modules.sys.entity.User;
import com.wlb365.financing.modules.sys.service.IDeptService;
import com.wlb365.financing.modules.sys.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 概述：
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2021/4/22
 */
@RestController
@RequestMapping("esign/query")
public class QueryEsignController {
    @Autowired
    private ESignService eSignService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("esign:query:index")
    @GetMapping(value = "index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView("esign/esign.html");
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(EsignQueryVO queryVO) {
        EsignQueryPageRes res = eSignService.page(queryVO);

        Page<QueryPageSignFlowDto> page = new Page<>();
        page.setCurrent(res.getCurrent());
        page.setSize(res.getSize());
        page.setTotal(res.getTotal());
        page.setRecords(res.getItems());

        return R.ok(page);
    }

    @GetMapping(value = "detail")
    public ModelAndView detail(String flowId) {
        ModelAndView mv = new ModelAndView("esign/esign_detail.html");
        mv.addObject("editInfo", eSignService.getSingFlow(flowId));
        return mv;
    }

    @RequiresPermissions("esign:query:checkView")
    @GetMapping(value = "checkView")
    public ModelAndView checkView(String flowId) {
        ModelAndView mv = new ModelAndView("esign/esign_check.html");
        mv.addObject("flowId", flowId);
        return mv;
    }

    @GetMapping(value = "queryCheckInfo")
    public R checkInfo(String flowId) {
        QuerySignAntPushInfoRes res = eSignService.querySignAntPushInfo(flowId);
        if (!ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            throw new AdminException("查证失败：" + res.getMsg() + "，" + res.getSubMsg());
        }
        Page<QuerySignAntPushInfoDto> page = new Page<>();

        page.setCurrent(1);
        page.setSize(res.getAntPushList().size());
        page.setTotal(res.getAntPushList().size());
        page.setRecords(res.getAntPushList());

        return R.ok(page);
    }

    /**
     * 获取签章流程详情.
     * @param signFlowId 签章流程id
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @GetMapping(value = "getSignFlow")
    public R getSignFlow(String signFlowId) {
        return R.ok(eSignService.getSingFlow(signFlowId));
    }

    /**
     * 获取签章流程URL.
     * @param signFlowId 签章流程id
     * @param orgId 机构id
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @GetMapping(value = "getSignFlowUrl")
    public R getSignFlow(String signFlowId, String orgId) {
        return R.ok(eSignService.getSingFlowUrl(signFlowId, orgId));
    }

    @RequiresPermissions("esign:query:getSignersView")
    @GetMapping(value = "getSignersView")
    public ModelAndView getSignersView(String flowId) {
        ModelAndView mv = new ModelAndView("esign/esign_signers.html");
        mv.addObject("flowId", flowId);
        return mv;
    }

    /**
     * 获取签署人列表.
     * @param flowId 签署流程id
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @GetMapping(value = "getSigners")
    public R getSigners(String flowId) {
        QueryFlowSignersRes res = eSignService.signersList(flowId);
        if (!ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            throw new AdminException("获取签署信息失败：" + res.getMsg() + "，" + res.getSubMsg());
        }
        Page<FlowSignerDto> page = new Page<>();
        if (res.getSigners() != null) {
            res.getSigners().forEach(flowSignerDto -> {
                User user = userService.getUserByAccount(flowSignerDto.getSignerAccountId());
                flowSignerDto.setOrgId(ObjectUtil.isEmpty(user) ? "" : user.getDeptId());
            });
            page.setSize(res.getSigners().size());
            page.setTotal(res.getSigners().size());
            page.setRecords(res.getSigners());
        }

        page.setCurrent(1);

        return R.ok(page);
    }

    /**
     * 下载流程文档.
     * @param flowId 签署流程id
     * @author zwh
     * @return com.wlb365.financing.common.dto.R
     **/
    @RequiresPermissions("esign:query:downFlowDoc")
    @GetMapping(value = "downFlowDoc")
    public R downFlowDoc(String flowId) {
        DownFlowDocRes res = eSignService.downFlowDoc(flowId);
        if (!ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            throw new AdminException("下载文档失败：" + res.getMsg() + "，" + res.getSubMsg());
        }
        return R.ok(res);

    }
}
